package com.shopping.mall.core.module.enterprise.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.enterprise.entity.Enterprise;
import com.shopping.mall.core.module.enterprise.service.EnterpriseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "企业用户")
@RestController
@RequestMapping("/enterprise")
public class EnterpriseController {
    @Autowired
    private EnterpriseService enterpriseService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<IPage<Enterprise>> getEnterprisePage(
        @RequestParam(name = "current", defaultValue = "1") Integer current,
        @RequestParam(name = "size", defaultValue = "10") Integer size,
        Enterprise enterprise) {
        IPage<Enterprise> enterprisePage=enterpriseService.getEnterprisePage(current,size,enterprise);
        return Result.OK(enterprisePage);
    }

    @ApiOperation("修改")
    @PutMapping
    public Result<Enterprise> update(@Validated(Enterprise.update.class) @RequestBody Enterprise enterprise) {
        enterpriseService.updateById(enterprise);
        return Result.OK();
    }

    @ApiOperation("新增")
    @PostMapping
    public Result<Object> add(@Validated(Enterprise.insert.class) @RequestBody Enterprise enterprise) {
        enterpriseService.save(enterprise);
        return Result.OK();
    }

    @ApiOperation("通过id查询")
    @GetMapping("/id")
    public Result<Enterprise> getById(@RequestParam String id){
        Enterprise enterprise=enterpriseService.getById(id);
        return Result.OK(enterprise);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public Result<List<Enterprise>> getAll(Enterprise enterprise){
        List<Enterprise> enterpriseList=enterpriseService.getAll(enterprise);
        return Result.OK(enterpriseList);
    }

    @ApiOperation("通过id删除")
    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id){
        return enterpriseService.deleteById(id);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public Result<Object> batchDelete(@RequestParam List<String> ids){
        return enterpriseService.batchDelete(ids);
    }
}
